module("library",package.seeall)

function GetFileExtName(filepath)
	local names = filepath:split("/");
	names = names[#names];
	if names == nil or names == "" then
		return "";
	end
	
	names = names:split(".");
	if names == nil or #names == 1 then
		return "";
	else
		return names[#names];
	end
end

function GetFilePath(filepath)
	if filepath:sub(1,1) == "@" then
		filepath = filepath:sub(2,#filepath);
	end
	local fp = string.reverse(filepath);
	local s,e = string.find(fp,"/")
	if s == nil then
		s,e = string.find(fp,"\\")
	end
	if s == nil then
		return "./";
	end

	return string.reverse(fp:sub(e,#fp));
end

function GetFileFileName(filepath)
	local fp = string.reverse(filepath);
	local s,e = string.find(fp,"/")
	if s == nil then
		s,e = string.find(fp,"\\")
	end
	if s == nil then
		return "";
	end

	return string.reverse(fp:sub(0,s-1));
end

function string:split(sep)
	local sep, fields = sep or ":", {}
	local pattern = string.format("([^(%s)]+)", sep)
	self:gsub(pattern, function(c) fields[#fields+1] = c end)
	return fields
end

function Split(str, delim, maxNb)
    -- Eliminate bad cases...
    if string.find(str, delim) == nil then
        return { str }
    end
    if maxNb == nil or maxNb < 1 then
        maxNb = 0    -- No limit
    end
    local result = {}
    local pat = "(.-)" .. delim .. "()"
    local nb = 0
    local lastPos
    for part, pos in string.gfind(str, pat) do
        nb = nb + 1
        result[nb] = part
        lastPos = pos
        if nb == maxNb then break end
    end
    -- Handle the last field
    if lastPos ~= string.len(str) then
        result[nb + 1] = string.sub(str, lastPos)
    end
    return result
end

function string:split3(sep)
	return Split(self,sep,2);
end

function trim(s)
  if s == nil then
	return s;
  end
  return (s:gsub("^%s*(.-)%s*$", "%1"))
end

function dump(o)
	if type(o) == 'table' then
		local s = '{ '
		for k,v in pairs(o) do
			if type(k) ~= 'number' then k = '"'..k..'"' end
			s = s .. k..' = ' .. dump(v) .. ','
		end
		return ( s .. '}\n')
	else
		return ( tostring(o))
	end
end
